
Page({
    data: {
      tabArray: ['商业贷款', '公积金贷款', '组合贷款'],
      tabIndex: 0,
      years: ["1年", "2年", "3年", "4年", "5年", "6年", "7年", "8年", "9年", "10年", "11年", "12年", "13年", "14年", "15年", "16年", "17年", "18年", "19年", "20年", "21年", "22年", "23年", "24年", "25年", "26年", "27年", "28年", "29年", "30年",], //按揭年数
      yearsIndex: 29, // 默认年数
      calculation: ["按房价总额","按单价"],  // 计算方式
      calculationIndex: 0, // 默认计算方式
      ratio: ["1成", "2成", "3成", "4成", "5成", "6成", "6.5成", "7成", "8成", "9成", "10成"], // 按揭成数
      ratioIndex: 7, // 默认按揭成数
      sdRate: '基准利率（4.9%）', // 商贷利率
      sdCount: '', // 房价总额
      unitPrice: '', // 单价
      temporaryData: '', // 临时数据
      providentFundRate: '基准利率（3.25%）', // 公积金利率
      providentFund: '', // 公积金总额
    },
    onLoad(t) {
      wx.showShareMenu({
        menus: ['shareAppMessage', 'shareTimeline']
      })
    },
    onReady() {},
    onShow() {},
    onHide() {},
    onUnload() {},
    onPullDownRefresh() {},
    onReachBottom() {},
    onShareAppMessage() {},
    chooseTabBar(e) { // 切换显示页
      let target = e.currentTarget.dataset;
      if (target.index !== this.data.tabIndex) {
        this.setData({
          tabIndex: Number(target.index),
          calculationIndex: 0,
          ratioIndex: 7
        })
      }
    },
    inputFocus(e) { // input聚焦事件
        let target = e.currentTarget.dataset;
        switch (target.type) {
          case 'providentFundRate':
          case 'sdRate':
            this.setData({
              [target.type]: parseFloat(e.detail.value.split('（')[1]),
              temporaryData: parseFloat(e.detail.value.split('（')[1])
            })
            break;
          default:
            this.setData({
              [target.type]: parseFloat(e.detail.value) || 0,
              temporaryData: parseFloat(e.detail.value) || 0
            })
            break;
        }
        
    },
    inputBlur(e) { // input失去焦点事件
      let target = e.currentTarget.dataset;
      if (!e.detail.value) {
        e.detail.value = this.data.temporaryData
      } else if (!parseFloat(e.detail.value)) {
        e.detail.value == 0 ? function(){}:this.showModal('请输入正确数值')
        e.detail.value = e.detail.value == 0 ? 0 : this.data[target.type]
      }
      switch (target.type) {
        case 'providentFundRate':
        case 'sdRate':
          this.setData({
            [target.type]: `基准利率（${parseFloat(e.detail.value)}%）`
          })
          break;
        case 'providentFund':
        case 'sdCount': 
          this.setData({
            [target.type]: `${parseFloat(e.detail.value)}万元`
          })
          break;
        default:
          this.setData({
            [target.type]: parseFloat(e.detail.value)
          })
          break;
      }
    },
    bindPickerChange(e) { // 选择picker
      let target = e.currentTarget.dataset, params = {};
      if (target.type === 'yearsIndex') {
        if (e.detail.value == 0) {
          params.sdRate = '基准利率（4.35%）';
          params.providentFundRate = '基准利率（2.75%）';
        } else if (e.detail.value >= 1 && e.detail.value <= 4 ) {
          params.sdRate = '基准利率（4.75%）';
          params.providentFundRate = '基准利率（2.75%）';
        } else if (e.detail.value > 4) {
          params.sdRate = '基准利率（4.9%）';
          params.providentFundRate = '基准利率（3.25%）';
        }
      }
      this.setData({
        [target.type]: Number(e.detail.value),
        ...params
      })
    },
    submit() { // 点击提交
      setTimeout(() => {
        this.submitComputed();
      }, 500);
    },
    submitComputed() { // 计算结果
      let { tabIndex, calculationIndex, sdRate, providentFundRate, providentFund, years, yearsIndex, ratio, ratioIndex, sdCount, unitPrice, area } = this.data;
      const sdInterestRate = parseFloat(sdRate.split('（')[1]) / 100 / 12, // 商贷月利率
            _providentFundRate = parseFloat(providentFundRate.split('（')[1]) / 100 / 12, // 公积金月利率
            month = parseInt(years[yearsIndex]) * 12, // 月数
            _ratio = parseFloat(ratio[ratioIndex]) / 10; // 成数 
      // console.log(sdInterestRate, month, _ratio);
      if (tabIndex === 0) { // 商贷
        if (calculationIndex === 0) { // 计算方式 => 按房价总额
          if (isNaN(parseFloat(sdCount))) {
            this.showModal('总额输入有误，请重新输入');
            return false;
          }
          var data = this.principalAndInteres({ sdCount, _ratio, sdInterestRate, month, years, yearsIndex });  // 等额本息
          var principalData = this.principal({ sdCount, _ratio, sdInterestRate, month, years, yearsIndex });  // 等额本金
          // console.log('商贷 ==> 按房价总额:', data, principalData);
          this.saveResult(JSON.stringify({principalAndInteres: data, principal: principalData}));
        } else { // 计算方式 => 按单价
          if (unitPrice == '' && area == '') {
            this.showModal('单价或面积输入有误，请重新输入');
            return
          }
          var unitData = this.unitPrincipalAndInteres({ _ratio, sdInterestRate, month });  // 等额本息
          var unitPrincipalData = this.unitPrincipal({ _ratio, sdInterestRate, month }); // 等额本金
          // console.log('商贷 ==> 按单价:', unitData, unitPrincipalData);
          this.saveResult(JSON.stringify({principalAndInteres: unitData, principal: unitPrincipalData}));
        }
      } else if (tabIndex === 1) { // 公积金
        if (calculationIndex === 0) { // 计算方式 => 按房价总额
          if (providentFund == '') {
            this.showModal('公积金总额输入有误，请重新输入');
            return
          }
          var data = this.providentFundPrincipalAndInteres({_ratio, month, _providentFundRate}); // 等额本息
          var principalData = this.providentFundPrincipal({_ratio, month, _providentFundRate}); // 等额本金
          // console.log('公积金 ==> 按房价总额:', data, principalData);
          this.saveResult(JSON.stringify({principalAndInteres: data, principal: principalData}));

        } else { // 计算方式 => 按单价
          if (unitPrice == '' && area == '') {
            this.showModal('单价或面积输入有误，请重新输入');
            return
          }
          var unitData = this.unitProvidentFundPrincipalAndInteres({_ratio, month, _providentFundRate}); // 等额本息
          var unitPrincipalData = this.unitProvidentFundPrincipal({_ratio, month, _providentFundRate}); // 等额本金
          // console.log('公积金 ==> 按单价:', unitData, unitPrincipalData);
          this.saveResult(JSON.stringify({principalAndInteres: unitData, principal: unitPrincipalData}));

        }
      } else {
        var data = this.combinationPrincipalAndInteres({month, _providentFundRate, sdInterestRate});
        var principalData = this.combinationPrincipal({month, _providentFundRate, sdInterestRate});
        // console.log('组合贷:', data, principalData);
        this.saveResult(JSON.stringify({principalAndInteres: data, principal: principalData}));
      }
    },
    showModal(msg) { // 信息提示
      wx.showModal({
          title: '提示',
          content: msg || '输入有误',
          success(res) {
              if (res.confirm) {
                  // console.log('用户点击确定')
              } else if (res.cancel) {
                  // console.log('用户点击取消')
              }
          }
      })
    },
    saveResult(data) { // 缓存结果
      try {
        wx.setStorageSync('_mortgage', data);
        wx.navigateTo({
          url: '/page/mortgage-result/mortgage-result',
        })
      } catch (error) {
        this.showModal(error);
      }
    },
    // -----------------商贷------------------
    principalAndInteres(params) { // 商贷 按房价总额 等额本息
      var { sdCount, _ratio, sdInterestRate, month, years, yearsIndex } = params;
      var _sdCount = parseFloat(sdCount) * _ratio;
      //1 + 月利率的期数方
      var _b = Math.pow(sdInterestRate + 1, month); 
      // 月还款
      var monthRepayment = _sdCount * sdInterestRate * [_b / (_b - 1)] * 10000;
      monthRepayment = monthRepayment.toFixed(2);
      //总利息
      var totalInterest = [month * monthRepayment - (_sdCount * 10000)] / 10000;
      totalInterest = totalInterest.toFixed(2);
      //还款总额
      var totalMoney = month * monthRepayment / 10000;
      totalMoney = totalMoney.toFixed(2);
      // 创建明细数据数组              
      var info = [];
      // 遍历期数
      for (let i = 0; i < month; i++) {
          // 创建一个对象 
          var obj = {};

          var _c = Math.pow((1 + sdInterestRate), ((i + 1) - 1));
          
          // 每月应还利息
          var everyMonthRepayment_lx = (_b - _c) / (_b - 1) * sdInterestRate * _sdCount * 10000;
          everyMonthRepayment_lx = everyMonthRepayment_lx.toFixed(2)
          // 每月应还本金
          var everyMonthRepayment_bj = _c / (_b - 1) * sdInterestRate * _sdCount * 10000;
          everyMonthRepayment_bj = everyMonthRepayment_bj.toFixed(2)
          // 本期剩余本金
          var ccc = 1 / Math.pow((1 + sdInterestRate), (month + 1 - (i + 1) )) * (1 - 1 / Math.pow((1 + sdInterestRate), (i + 1))) / (1 - 1 / (1 + sdInterestRate)) * monthRepayment;
          var result = (_sdCount * 10000 - ccc).toFixed(2);
          // 对象赋值
          obj.monthRepayment = monthRepayment;
          obj.everyMonthRepayment_lx = everyMonthRepayment_lx;
          obj.everyMonthRepayment_bj = everyMonthRepayment_bj;
          obj.result = result;
          obj.year = years[yearsIndex];
          info.push(obj)
      }
      var data = {
        totalLoans: _sdCount, // 商贷总额
        monthRepayment, //月还款
        totalInterest,//总利息
        totalMoney,//还款总额
        month,// 期数
        info,
        dk_type: '商业贷款',
        calculation: "房价总额",
        hk_Method: "等额本息"
      };
      return data;
    },
    principal(params) { // 商贷 按房价总额 等额本金
      var { sdCount, _ratio, sdInterestRate, month, years, yearsIndex } = params;
      var totalLoans = parseFloat(sdCount) * _ratio;
      // 总利息
      var totalInterest = totalLoans * _ratio * sdInterestRate * (month + 1) / 2;
      // 还款总额
      var totalMoney = totalInterest + parseInt(totalLoans);
      //月均还款  总利息 + 总代额 / 期数
      var monthRepayment = (((totalInterest + parseInt(totalLoans)) / month) * 10000).toFixed(2);
      // 当月应还后半部分
      var curMonthEnd = 1 / month + 1 * sdInterestRate;
      //当月应还
      var curMonth = totalLoans * curMonthEnd * 10000;
      curMonth = curMonth.toFixed(2);
      // 创建明细数据数组
      var info = [];
      var diminishing = 0;
      // 遍历期数
      for (let i = 0; i < month; i++) {
          // 创建一个对象 
          var obj = {};
          // 本期还款
          var monthRepayment = (totalLoans * 10000) * (1 / month + (1 - (i - 1 + 1) / month) * sdInterestRate)
          monthRepayment = monthRepayment.toFixed(2)
          if (i == 1) { // 每月递减金额
            diminishing = (info[i-1].monthRepayment-monthRepayment).toFixed(2);
          }
          // 本期本金
          var everyMonthRepayment_bj = (totalLoans * 10000) / month;
          everyMonthRepayment_bj = everyMonthRepayment_bj.toFixed(2)
          // 本期剩余
          var _result = (totalLoans * 10000) - i * ((totalLoans * 10000) / month);
          var result = (totalLoans * 10000) - (i + 1) * ((totalLoans * 10000) / month);
          result = result.toFixed(2)
          // 本期利息
          var everyMonthRepayment_lx = _result * sdInterestRate;
          everyMonthRepayment_lx = everyMonthRepayment_lx.toFixed(2)

          obj.monthRepayment = monthRepayment; 
          obj.everyMonthRepayment_lx = everyMonthRepayment_lx; 
          obj.everyMonthRepayment_bj = everyMonthRepayment_bj; 
          obj.result = result; 
          obj.year = years[yearsIndex];
          info.push(obj)
      }
      var data = {
          // 商贷总额
          totalLoans,
          // 月均还款
          monthRepayment,
          //总利息
          totalInterest: totalInterest.toFixed(2),
          //还款总额
          totalMoney,
          month,// 期数
          curMonth, // 首期应还
          info,
          dk_type: '商业贷款',
          calculation: "房价总额",
          hk_Method: "等额本金",
          diminishing: diminishing, // 每月递减
      };
      return data;
    },
    unitPrincipalAndInteres(params) { // 商贷 按单价 等额本息
      var {unitPrice, area, years, yearsIndex} = this.data;
      var {_ratio, sdInterestRate, month} = params;

      var a = sdInterestRate;
      // 贷款总额  = 单价 * 面积 * 成数
      var totalLoans = unitPrice * area * _ratio / 10000;
      
      var _b = Math.pow(a + 1, month);
      // 每月应还
      var monthRepayment = totalLoans * a * [_b / (_b - 1)] * 10000;
      monthRepayment = monthRepayment.toFixed(2);
      //总利息
      var totalInterest = [month * monthRepayment - (totalLoans * 10000)] / 10000;
      totalInterest = totalInterest.toFixed(2);
      //还款总额
      var totalMoney = month * monthRepayment / 10000;
      totalMoney = totalMoney.toFixed(2);
      // 创建明细数据数组
      var info = [];
      // 遍历期数
      for (let i = 0; i < month; i++) {
          // 创建一个对象 
          var obj = {};

          var _c = Math.pow((1 + a), ((i + 1) - 1));
          // 每月应还利息
          var everyMonthRepayment_lx = (_b - _c) / (_b - 1) * a * totalLoans * 10000;
          everyMonthRepayment_lx = everyMonthRepayment_lx.toFixed(2)
          // 每月应还本金
          var everyMonthRepayment_bj = _c / (_b - 1) * a * totalLoans * 10000;
          everyMonthRepayment_bj = everyMonthRepayment_bj.toFixed(2)
          // 本期剩余本金
          var ccc = 1 / Math.pow((1 + a), (month + 1 - (i + 1))) * (1 - 1 / Math.pow((1 + a), (i + 1))) / (1 - 1 / (1 + a)) * monthRepayment;
          var result = (totalLoans * 10000 - ccc).toFixed(2);
          // 对象赋值
          obj.monthRepayment = monthRepayment;
          obj.everyMonthRepayment_lx = everyMonthRepayment_lx;
          obj.everyMonthRepayment_bj = everyMonthRepayment_bj;
          obj.result = result;
          obj.year = years[yearsIndex];
          info.push(obj)
      }
      var data = {
          totalLoans, // 商贷总额
          monthRepayment, //月还款
          totalInterest,//总利息
          totalMoney,//还款总额
          month,// 期数
          dk_type: '商业贷款',
          calculation: "单价",
          hk_Method: "等额本息",
          info,
      };
      return data;
    },
    unitPrincipal(params) { // 商贷 按单价 等额本金
      var {unitPrice, area, years, yearsIndex} = this.data;
      var {_ratio, sdInterestRate, month} = params;
      var a = sdInterestRate;

      // 一 贷款总额  = 单价 * 面积 * 成数
      var totalLoans = unitPrice * area * _ratio / 10000;

      // 总利息
      var totalInterest = totalLoans * a * (month + 1) / 2;
      // 还款总额
      var totalMoney = totalInterest + parseInt(totalLoans);
      //月均还款  总利息 + 总代额 / 期数
      var monthRepayment = (((totalInterest + parseInt(totalLoans)) / month) * 10000).toFixed(2);
      // 当月应还后半部分
      var curMonthEnd = 1 / month + 1 * a;
      //当月应还
      var curMonth = totalLoans * curMonthEnd * 10000;
      curMonth = curMonth.toFixed(2);
      var info = [];
      var diminishing = 0;
      // 遍历期数
      for (let i = 0; i < month; i++) {
          // 创建一个对象 
          var obj = {};
          // 本期还款
          var _monthRepayment = (totalLoans * 10000) * (1 / month + (1 - (i - 1 + 1) / month) * a)
          _monthRepayment = _monthRepayment.toFixed(2)
          if (i == 1) { // 每月递减金额
            diminishing = (info[i-1].monthRepayment-_monthRepayment).toFixed(2);
          }
          // 本期本金
          var everyMonthRepayment_bj = (totalLoans * 10000) / month;
          everyMonthRepayment_bj = everyMonthRepayment_bj.toFixed(2)
          // 本期剩余
          var _result = (totalLoans * 10000) - i * ((totalLoans * 10000) / month);
          var result = (totalLoans * 10000) - (i + 1) * ((totalLoans * 10000) / month);
          result = result.toFixed(2)
          // 本期利息
          var everyMonthRepayment_lx = _result * a;
          everyMonthRepayment_lx = everyMonthRepayment_lx.toFixed(2)

          obj.monthRepayment = _monthRepayment;
          obj.everyMonthRepayment_lx = everyMonthRepayment_lx;
          obj.everyMonthRepayment_bj = everyMonthRepayment_bj;
          obj.result = result;
          obj.year = years[yearsIndex];
          info.push(obj)
      }
      var data = {
          totalLoans,
          // 月均还款
          monthRepayment,
          //总利息
          totalInterest: totalInterest.toFixed(2),
          //还款总额
          totalMoney,
          month,// 期数
          curMonth, // 首期应还
          dk_type: '商业贷款',
          calculation: "单价",
          hk_Method: "等额本金",
          info,
          diminishing: diminishing, // 每月递减
      };
      return data;
    },
    // ===================商贷==================

    // -------------------公积金-----------------
    providentFundPrincipalAndInteres(params) { // 公积金 按房价总额 等额本息
      var { providentFund, years, yearsIndex } = this.data;
      var { _ratio, month, _providentFundRate } = params;

      var totalLoans = parseFloat(providentFund) * _ratio;
      var b = _providentFundRate;
      //1 + 月利率的期数方
      var _b = Math.pow(b + 1, month);
      // 每月应还
      var monthRepayment = totalLoans * b * [_b / (_b - 1)] * 10000;
      monthRepayment = monthRepayment.toFixed(2);
      //总利息
      var totalInterest = [month * monthRepayment - (totalLoans * 10000)] / 10000;
      totalInterest = totalInterest.toFixed(2);
      //还款总额
      var totalMoney = month * monthRepayment / 10000;
      totalMoney = totalMoney.toFixed(2);
      var info = [];
      // 遍历期数
      for (let i = 0; i < month; i++) {
          // 创建一个对象 
          var obj = {};
          var _c = Math.pow((1 + b), ((i + 1) - 1));
          
          // 每月应还利息
          var everyMonthRepayment_lx = (_b - _c) / (_b - 1) * b * totalLoans * 10000;
          everyMonthRepayment_lx = everyMonthRepayment_lx.toFixed(2)
          // 每月应还本金
          var everyMonthRepayment_bj = _c / (_b - 1) * b * totalLoans * 10000;
          everyMonthRepayment_bj = everyMonthRepayment_bj.toFixed(2)
          // 本期剩余本金
          var ccc = 1 / Math.pow((1 + b), (month + 1 - (i + 1))) * (1 - 1 / Math.pow((1 + b), (i + 1))) / (1 - 1 / (1 + b)) * monthRepayment;
          var result = (totalLoans * 10000 - ccc).toFixed(2);
          obj.monthRepayment = monthRepayment;
          obj.everyMonthRepayment_lx = everyMonthRepayment_lx;
          obj.everyMonthRepayment_bj = everyMonthRepayment_bj;
          obj.result = result;
          obj.year = years[yearsIndex];
          info.push(obj)
      }
      var data = {
        totalLoans, // 商贷总额
        monthRepayment, //月还款
        totalInterest,//总利息
        totalMoney,//还款总额
        month,// 期数
        dk_type: '公积金贷款',
        calculation: "房价总额",
        hk_Method: "等额本息",
        info,
      };
      return data;
    },
    providentFundPrincipal(params) { // 公积金 按房价总额 等额本金
      var { providentFund, years, yearsIndex } = this.data;
      var { _ratio, month, _providentFundRate } = params;
      var totalLoans = parseFloat(providentFund) * _ratio;
      var b = _providentFundRate;

      // 总利息
      var totalInterest = totalLoans * b * (month + 1) / 2;
      // 还款总额
      var totalMoney = totalInterest + parseInt(totalLoans);
      //月均还款  总利息 + 总代额 / 期数
      var monthRepayment = (((totalInterest + parseInt(totalLoans)) / month) * 10000).toFixed(2);
      // 当月应还后半部分
      var curMonthEnd = 1 / month + 1 * b;
      //当月应还
      var curMonth = totalLoans * curMonthEnd * 10000;
      curMonth = curMonth.toFixed(2);
      var info = [];
      var diminishing = 0;
      // 遍历期数
      for (let i = 0; i < month; i++) {
          // 创建一个对象 
          var obj = {};
          // 本期还款
          var monthRepayment = (totalLoans * 10000) * (1 / month + (1 - (i - 1 + 1) / month) * b)
          monthRepayment = monthRepayment.toFixed(2)
          if (i == 1) { // 每月递减金额
            diminishing = (info[i-1].monthRepayment-monthRepayment).toFixed(2);
          }
          // 本期本金
          var everyMonthRepayment_bj = (totalLoans * 10000) / month;
          everyMonthRepayment_bj = everyMonthRepayment_bj.toFixed(2)
          // 本期剩余
          var _result = (totalLoans * 10000) - i * ((totalLoans * 10000) / month);
          var result = (totalLoans * 10000) - (i + 1) * ((totalLoans * 10000) / month);
          result = result.toFixed(2)
          // 本期利息
          var everyMonthRepayment_lx = _result * b;
          everyMonthRepayment_lx = everyMonthRepayment_lx.toFixed(2)
          obj.monthRepayment = monthRepayment;
          obj.everyMonthRepayment_lx = everyMonthRepayment_lx;
          obj.everyMonthRepayment_bj = everyMonthRepayment_bj;
          obj.result = result;
          obj.year = years[yearsIndex];
          info.push(obj)
      }
      var data = {
          // 商贷总额
          totalLoans,
          // 月均还款
          monthRepayment,
          //总利息
          totalInterest: totalInterest.toFixed(2),
          //还款总额
          totalMoney,
          month,// 期数
          curMonth, // 首期应还
          info,
          hk_Method: "等额本金",
          dk_type: '公积金贷款',
          calculation: "房价总额",
          diminishing: diminishing, // 每月递减
      };
      return data;
    },
    unitProvidentFundPrincipalAndInteres(params) { // 公积金 按单价 等额本息
      var { unitPrice, area, years, yearsIndex } = this.data;
      var { _ratio, month, _providentFundRate } = params;
      var b = _providentFundRate;

      var homePrice = unitPrice * area;
      // 一 贷款总额  = 单价 * 面积 * 成数
      var totalLoans = unitPrice * area * _ratio / 10000; 
      var _b = Math.pow(b + 1, month);
      // 每月应还
      var monthRepayment = totalLoans * b * [_b / (_b - 1)] * 10000;
      monthRepayment = monthRepayment.toFixed(2);
      //总利息
      var totalInterest = [month * monthRepayment - (totalLoans * 10000)] / 10000;
      totalInterest = totalInterest.toFixed(2);
      //还款总额
      var totalMoney = month * monthRepayment / 10000;
      totalMoney = totalMoney.toFixed(2);
      var info = [];
      // 遍历期数
      for (let i = 0; i < month; i++) {
          // 创建一个对象 
          var obj = {};
          var _c = Math.pow((1 + b), ((i + 1) - 1));
          
          // 每月应还利息
          var everyMonthRepayment_lx = (_b - _c) / (_b - 1) * b * totalLoans * 10000;
          everyMonthRepayment_lx = everyMonthRepayment_lx.toFixed(2)
          // 每月应还本金
          var everyMonthRepayment_bj = _c / (_b - 1) * b * totalLoans * 10000;
          everyMonthRepayment_bj = everyMonthRepayment_bj.toFixed(2)
          // 本期剩余本金
          var ccc = 1 / Math.pow((1 + b), (month + 1 - (i + 1))) * (1 - 1 / Math.pow((1 + b), (i + 1))) / (1 - 1 / (1 + b)) * monthRepayment;
          var result = (totalLoans * 10000 - ccc).toFixed(2);
          obj.monthRepayment = monthRepayment;
          obj.everyMonthRepayment_lx = everyMonthRepayment_lx;
          obj.everyMonthRepayment_bj = everyMonthRepayment_bj;
          obj.result = result;
          obj.year = years[yearsIndex];
          info.push(obj)
      }
      var data = {
          homePrice,
          totalLoans, // 商贷总额
          monthRepayment, //月还款
          totalInterest,//总利息
          totalMoney,//还款总额
          month,// 期数
          hk_Method: "等额本息",
          dk_type: '公积金贷款',
          calculation: "单价",
          info,
      };
      return data;
    },
    unitProvidentFundPrincipal(params) { // 公积金 按单价 等额本金
      var { unitPrice, area, years, yearsIndex } = this.data;
      var { _ratio, month, _providentFundRate } = params;
      var b = _providentFundRate;

      var homePrice = unitPrice * area;
      // 一 贷款总额  = 单价 * 面积 * 成数
      var totalLoans = unitPrice * area * _ratio / 10000; 
      // 总利息
      var totalInterest = totalLoans * b * (month + 1) / 2;
      // 还款总额
      var totalMoney = totalInterest + parseInt(totalLoans);
      //月均还款  总利息 + 总代额 / 期数
      var monthRepayment = (((totalInterest + parseInt(totalLoans)) / month) * 10000).toFixed(2);
      // 当月应还后半部分
      var curMonthEnd = 1 / month + 1 * b;
      //当月应还
      var curMonth = totalLoans * curMonthEnd * 10000;
      curMonth = curMonth.toFixed(2);
      var info = [];
      var diminishing = 0;
      // 遍历期数
      for (let i = 0; i < month; i++) {
          // 创建一个对象 
          var obj = {};
          // 本期还款
          var _monthRepayment = (totalLoans * 10000) * (1 / month + (1 - (i - 1 + 1) / month) * b)
          _monthRepayment = _monthRepayment.toFixed(2)
          if (i == 1) { // 每月递减金额
            diminishing = (info[i-1].monthRepayment-_monthRepayment).toFixed(2);
          }
          // 本期本金
          var everyMonthRepayment_bj = (totalLoans * 10000) / month;
          everyMonthRepayment_bj = everyMonthRepayment_bj.toFixed(2)
          // 本期剩余
          var _result = (totalLoans * 10000) - i * ((totalLoans * 10000) / month);
          var result = (totalLoans * 10000) - (i + 1) * ((totalLoans * 10000) / month);
          result = result.toFixed(2)
          // 本期利息
          var everyMonthRepayment_lx = _result * b;
          everyMonthRepayment_lx = everyMonthRepayment_lx.toFixed(2)
          obj.monthRepayment = _monthRepayment;
          obj.everyMonthRepayment_lx = everyMonthRepayment_lx;
          obj.everyMonthRepayment_bj = everyMonthRepayment_bj;
          obj.result = result;
          obj.year = years[yearsIndex];
          info.push(obj)
      }
      var data = {
          // 商贷总额
          homePrice,
          hk_Method: "等额本金",
          dk_type: '公积金贷款',
          calculation: "单价",
          totalLoans,
          // 月均还款
          monthRepayment,
          //总利息
          totalInterest: totalInterest.toFixed(2),
          //还款总额
          totalMoney,
          month,// 期数
          curMonth, // 首期应还
          info,
          diminishing: diminishing, // 每月递减
      };
      return data;
    },
    // ===================公积金==================

    // -------------------组合贷------------------
    combinationPrincipalAndInteres(params) { // 等额本息
      var { providentFund, sdCount, years, yearsIndex } = this.data;
      var { month, _providentFundRate, sdInterestRate } = params;
      var a = sdInterestRate;
      var b = _providentFundRate;
      var totalLoans = parseFloat(sdCount) || 0;
      var gjijinloan = parseFloat(providentFund) || 0;

      var _b = Math.pow(a + 1, month);
      var _B = Math.pow(b + 1, month);
      // 每月应还
      var monthRepayment = totalLoans * a * [_b / (_b - 1)] * 10000;
      var _monthRepayment = gjijinloan * b * [_B / (_B - 1)] * 10000;
      var redult_monthRepayment = (monthRepayment + _monthRepayment).toFixed(2);
      var totalInterest = [month * monthRepayment - (totalLoans * 10000)] / 10000;
      var _totalInterest = [month * _monthRepayment - (gjijinloan * 10000)] / 10000;
      totalInterest = (totalInterest + _totalInterest).toFixed(2);
      var totalMoney = month * monthRepayment / 10000; 
      var _totalMoney = month * _monthRepayment / 10000;
      totalMoney = (totalMoney + _totalMoney).toFixed(2); //还款总额
      var result_totalLoans =  Number(gjijinloan) + Number(totalLoans)
      var info = [];
      // 遍历期数
      for (let i = 0; i < month; i++) {
          // 创建一个对象 
          var obj = {};
          var s_c = Math.pow((1 + a), ((i + 1) - 1));
          var g_c = Math.pow((1 + b), ((i + 1) - 1));
          // 每月应还利息
          var s_everyMonthRepayment_lx = (_b - s_c) / (_b - 1) * a * totalLoans * 10000;
          var g_everyMonthRepayment_lx = (_B - g_c) / (_B - 1) * b * totalLoans * 10000;
          var everyMonthRepayment_lx = (s_everyMonthRepayment_lx + g_everyMonthRepayment_lx).toFixed(2)
          // 每月应还本金
          var s_everyMonthRepayment_bj = s_c / (_b - 1) * a * totalLoans * 10000;
          var g_everyMonthRepayment_bj = g_c / (_B - 1) * b * totalLoans * 10000;
          var everyMonthRepayment_bj = (s_everyMonthRepayment_bj + g_everyMonthRepayment_bj).toFixed(2)
          // 本期剩余本金
          var s_ccc = 1 / Math.pow((1 + a), (month + 1 - (i + 1))) * (1 - 1 / Math.pow((1 + a), (i + 1))) / (1 - 1 / (1 + a)) * monthRepayment;
          var s_result = (totalLoans * 10000 - s_ccc).toFixed(2);
          var g_ccc = 1 / Math.pow((1 + b), (month + 1 - (i + 1))) * (1 - 1 / Math.pow((1 + b), (i + 1))) / (1 - 1 / (1 + b)) * _monthRepayment;
          var g_result = (totalLoans * 10000 - g_ccc).toFixed(2);
          var result = (Number(s_result) + Number(g_result)).toFixed(2);
          obj.monthRepayment = redult_monthRepayment;
          obj.everyMonthRepayment_lx = everyMonthRepayment_lx;
          obj.everyMonthRepayment_bj = everyMonthRepayment_bj;
          obj.result = result;
          obj.year = years[yearsIndex];
          info.push(obj)
      }
      var data = {
          totalLoans: Number(gjijinloan) + Number(totalLoans), // 商贷总额
          monthRepayment: redult_monthRepayment,
          totalInterest,//总利息
          totalMoney,//还款总额
          month,// 期数
          hk_Method: "等额本息",
          dk_type: '组合贷款',
          info,
      };
      return data;
    },
    combinationPrincipal(params) { // 等额本金
      var { providentFund, sdCount, years, yearsIndex } = this.data;
      var { month, _providentFundRate, sdInterestRate } = params;
      var a = sdInterestRate;
      var b = _providentFundRate;
      var totalLoans = parseFloat(sdCount) || 0;
      var gjijinloan = parseFloat(providentFund) || 0;

      // 总利息
      var totalInterest = totalLoans * a * (month + 1) / 2;
      var _totalInterest = gjijinloan * b * (month + 1) / 2;
      var result_totalInterest = (totalInterest + _totalInterest).toFixed(2);
      // 还款总额
      var totalMoney = Number(gjijinloan) + Number(totalLoans) + Number(totalInterest + _totalInterest);
      //月均还款  总利息 + 总代额 / 期数
      var monthRepayment = (totalMoney / month * 10000).toFixed(2);
      // 当月应还后半部分
      var curMonthEnd = 1 / month + 1 * a;
      var _curMonthEnd = 1 / month + 1 * b;
      //当月应还
      var curMonth = totalLoans * curMonthEnd * 10000;
          // curMonth = curMonth.toFixed(2);
      var _curMonth = gjijinloan * _curMonthEnd * 10000;
          // _curMonth = curMonth.toFixed(2);
      var result_curMonth = (curMonth + _curMonth).toFixed(2);
      var info = [];
      var diminishing = 0;
      // 遍历期数
      for (let i = 0; i < month; i++) {
          // 创建一个对象 
          var obj = {};
          // 本期还款
          var s_monthRepayment = (totalLoans * 10000) * (1 / month + (1 - (i - 1 + 1) / month) * a);
          var g_monthRepayment = (totalLoans * 10000) * (1 / month + (1 - (i - 1 + 1) / month) * b);
          var _monthRepayment = (s_monthRepayment + g_monthRepayment).toFixed(2)
          if (i == 1) { // 每月递减金额
            diminishing = (info[i-1].monthRepayment-_monthRepayment).toFixed(2);
          }
          // 本期本金
          var everyMonthRepayment_bj = ((Number(gjijinloan) + Number(totalLoans)) * 10000) / month;
          everyMonthRepayment_bj = everyMonthRepayment_bj.toFixed(2)
          // 本期剩余
          var _result = ((Number(gjijinloan) + Number(totalLoans)) * 10000) - i * (((Number(gjijinloan) + Number(totalLoans)) * 10000) / month);
          var result = ((Number(gjijinloan) + Number(totalLoans)) * 10000) - (i + 1) * (((Number(gjijinloan) + Number(totalLoans)) * 10000) / month);
          result = result.toFixed(2)
          // 本期利息
          var s_everyMonthRepayment_lx = _result * a;
          var g_everyMonthRepayment_lx = _result * b;
          var everyMonthRepayment_lx = ((s_everyMonthRepayment_lx + g_everyMonthRepayment_lx) / 2).toFixed(2)
          obj.monthRepayment = _monthRepayment;
          obj.everyMonthRepayment_lx = everyMonthRepayment_lx;
          obj.everyMonthRepayment_bj = everyMonthRepayment_bj;
          obj.result = result;
          obj.year = years[yearsIndex];
          info.push(obj)
      }
      var data = {
          // 商贷总额
          totalLoans: Number(gjijinloan) + Number(totalLoans),
          // 月均还款
          monthRepayment,
          //总利息
          totalInterest: result_totalInterest,
          //还款总额
          totalMoney: totalMoney.toFixed(2),
          month,// 期数
          curMonth: result_curMonth, // 首期应还
          hk_Method: "等额本金",
          dk_type: '组合贷款',
          info,
          diminishing: diminishing, // 每月递减
      };
      return data
    }
    // ===================组合贷==================
  })
